import { Component, OnInit, Input, ViewEncapsulation } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { ConfigMetaBase } from 'app/modules/zone/utils/config-meta-base';
import { ConfigControlService } from 'app/modules/zone/services/config-control.service';
import { ConfigMetaService } from 'app/modules/zone/services/config-meta.service';

@Component({
  selector: 'm-advanced-setting-form',
  templateUrl: './advanced-setting-form.component.html',
  styleUrls: ['../styles.scss'],
})
export class AdvancedSettingFormComponent implements OnInit {

  @Input() advancedSettingForm: FormGroup;
  @Input() metas: ConfigMetaBase<string>[];
  // advancedSettingForm: FormGroup = new FormGroup({});
  metasGroupByName: object = {};
  metasGroupByName_checkbox: object = {};
  checkboxTips = '1020以前的版本，控制中心无“通知”功能；1021以前的版本，控制中心“Union ID”名称为“网络帐户”；“蓝牙”和“数位板”，需要终端硬件支持，功能入口才能显示';

  constructor(
    private mcs: ConfigMetaService,
    private ccs: ConfigControlService,
  ) {
  }

  async ngOnInit() {

    const { metasGroupByName, metasGroupByName_checkbox } = this.toGroup(this.metas);
    this.metasGroupByName = metasGroupByName;
    this.metasGroupByName_checkbox = metasGroupByName_checkbox;

  }

  toGroup(metas: ConfigMetaBase<string>[]) {
    const metasGroupByName = {};
    // 需要将 checkbox 类型单独放在一个 form-group 中
    const metasGroupByName_checkbox = [];
    metas.forEach((meta) => {
      if (meta.controlType === 'checkbox') {
        metasGroupByName_checkbox.push(meta);
      } else {
        if (!metasGroupByName.hasOwnProperty(meta.group_name)) {
          metasGroupByName[meta.group_name] = [];
        }
        metasGroupByName[meta.group_name].push(meta);
      }
    });

    return { metasGroupByName, metasGroupByName_checkbox };
  }

}
